/*Diego Lima de Freitas RA: 317632
 *Yuri Souza Padua RA: 317420
 * 
 * Classe que trata o comando if
 */
package AST;

import java.util.ArrayList;

public class IfCommand extends Command {

    private Expr expr;
    private ArrayList<Command> thenPart, elsePart;

    public IfCommand(Expr expr, ArrayList<Command> thenPart, ArrayList<Command> elsePart) {
        this.expr = expr;
        this.thenPart = thenPart;
        this.elsePart = elsePart;
    }

    @Override
    public void genC(PW pw) {

        pw.print("if ( ");
        expr.genC(pw, false);
        pw.out.println(" ) ");
        if (thenPart != null) {
            pw.add();
            for (int i = 0; i < thenPart.size(); i++) {
                thenPart.get(i).genC(pw);
            }
            pw.sub();
            //pw.println("}");
        }
        if (elsePart != null) {
            pw.println("else");
            pw.add();
            for (int i = 0; i < elsePart.size(); i++) {
                elsePart.get(i).genC(pw);
            }
            pw.sub();
            //pw.println("}");
        }
    }
}
